#!/usr/bin/env python3
import time
import kociemba
def _KociembaTrans(KociembaCode):
    _Solution = []
    Cnt_Solution = 0
    Step_temp = ''
    for i in range(len(KociembaCode)):
        if (KociembaCode[i] != ' '):
            if (KociembaCode[i] == '\''):
                Step_temp += '3'
            elif (KociembaCode[i] == '2'):
                Step_temp += '2'
            else:
                Step_temp += KociembaCode[i]
        else:
            _Solution.append(Step_temp)
            Step_temp = ''
            Cnt_Solution += 1
    _Solution.append(Step_temp)
    for i in range(len(_Solution)):
        if (len(_Solution[i]) == 1):
            _Solution[i] += '1'
    return _Solution
def _SolutionTrans(Solution):
    Standard = 'UDFBRL'
    transtable = {'U' :'DUFBLR',#U U->D D->U L->R R->L
                  'D' :'UDFBRL',
                  'F' :'UDBFLR',#F F->B B->F L->R R->L
                  'L' :'LRFBUD',#L L->D D->R R->U U->L
                  'R' :'RLFBDU',#R R->D D->L L->U U->R
                  'B' :'UDFBRL'}
    for i in range(len(Solution)):
        for j in range((i+1),len(Solution)):
            for face in range(len(Standard)):
                if (Solution[j][0] == Standard[face]):
                    Solution[j] = Solution[j].replace(Standard[face],transtable[Solution[i][0]][face])
                    break
    return Solution

def _SolutionTransAndOptimize(Solution):#需要修改
    Standard = 'UDFBRL'
    transtable = {'U' :('DUFBLR',0.4),#U U->D D->U L->R R->L
                  'D' :('UDFBRL',0),
                  'F' :('UDBFLR',0.4),#F F->B B->F L->R R->L

                  'I' :('LRFBUD',0.6),#L L->D D->R R->U U->L
                  'l' :('UDLRFB',0.6),#l L->B B->R R->F F->L

                  'Z' :('RLFBDU',0.6),#R R->D D->L L->U U->R
                  'r' :('UDRLBF',0.6),#r R->B B->L L->F F->R
                  'B' :('UDFBRL',0)}
    cost = 0
    cost_l = 0
    cost_L = 0
    Solution_l = []
    Solution_L = []
    Solution_temp_l = []
    Solution_temp_L = []
    for i in range(len(Solution)):
        if Solution[i][0] == 'L':
            Solution_l = Solution[i:len(Solution)]
            Solution_l[0] = Solution_l[0].replace('L','l')
            Solution_L = Solution[i:len(Solution)]
            Solution_L[0] = Solution_L[0].replace('L','I')
            Solution_temp_l,cost_l = _SolutionTransAndOptimize(Solution_l)
            Solution_temp_L,cost_L = _SolutionTransAndOptimize(Solution_L)
            if cost_L < cost_l:
                # print('low:'+str(cost_L)+' high:'+str(cost_l))
                # print(Solution_temp_L)
                _Solution = Solution[0:i]
                if (Solution_temp_L[0][0] == 'I'):
                    Solution_temp_L[0] = Solution_temp_L[0].replace('I','L')
                _Solution = _Solution + Solution_temp_L
                cost = cost+cost_L
                return _Solution,cost
            else:
                # print('1low:'+str(cost_l)+' 1high:'+str(cost_L))
                # print(Solution_temp_l)
                _Solution = Solution[0:i]
                if (Solution_temp_l[0][0] == 'I'):
                    Solution_temp_l[0] = Solution_temp_l[0].replace('I','L')
                _Solution = _Solution + Solution_temp_l
                cost = cost+cost_l
                return _Solution,cost
        elif Solution[i][0] == 'R':
            Solution_r = Solution[i:len(Solution)]
            Solution_r[0] = Solution_r[0].replace('R','r')
            Solution_R = Solution[i:len(Solution)]
            Solution_R[0] = Solution_R[0].replace('R','Z')
            Solution_temp_r,cost_r = _SolutionTransAndOptimize(Solution_r)
            Solution_temp_R,cost_R = _SolutionTransAndOptimize(Solution_R)
            if cost_R < cost_r:
                # print('2low:'+str(cost_R)+' 2high:'+str(cost_r))
                _Solution = Solution[0:i]
                if (Solution_temp_R[0][0] == 'Z'):
                    Solution_temp_R[0] = Solution_temp_R[0].replace('Z','R')
                _Solution = _Solution + Solution_temp_R 
                cost = cost+cost_R
                return _Solution,cost
            else:
                # print('3low:'+str(cost_r)+' 3high:'+str(cost_R))
                _Solution = Solution[0:i]
                if (Solution_temp_r[0][0] == 'Z'):
                    Solution_temp_r[0] = Solution_temp_r[0].replace('Z','R')
                _Solution = _Solution + Solution_temp_r
                cost = cost+cost_r
                return _Solution,cost
        else:
            cost = cost + transtable[Solution[i][0]][1]
            for j in range((i+1),len(Solution)):
                for face in range(len(Standard)):
                    if (Solution[j][0] == Standard[face]):
                        Solution[j] = Solution[j].replace(Standard[face],transtable[Solution[i][0]][0][face])
                        break
    for i in range(len(Solution)):
        # cost = cost + transtable[Solution[i][0]][1]
        if (Solution[i][0] == 'Z'):
            Solution[i] = Solution[i].replace('Z','R')
        if (Solution[i][0] == 'I'):
            Solution[i] = Solution[i].replace('I','L')
    
    return Solution, cost
def TransToRealSolution(KociembaCode):
    solution = _KociembaTrans(KociembaCode)
    # print(solution)
    return _SolutionTransAndOptimize(solution)

if __name__ == '__main__':
    # solution = TransToRealSolution('F\' D')
    # print(solution)
    Solution = kociemba.solve('LLLDUDDUULRDLRUDLDBBBFFBFFBURRUDUURRULLDLDRRRFFFFBBFBB')
    solution = _KociembaTrans(Solution)
    print(solution)
    sss = []
    sss = sss+ solution
    ssssss = _SolutionTrans(sss)
    print('1'+str(solution))
    print(ssssss)
    wwww = solution
    solu  =_SolutionTransAndOptimize(wwww)
    print(solu)
            